package com.spd.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.spd.shop.entity.Contract;
import com.spd.shop.service.IContractService;
import com.spd.shop.utils.Main;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 孙沛栋
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/contract")
public class ContractController extends Main {



    @Resource
    private IContractService contractService;


    private static final String FONT_PATH="C:\\Windows\\Fonts\\simsun.ttc,0";


    //新增或者更新
    @PostMapping
    public boolean save(@RequestBody Contract contract) {
        return contractService.saveOrUpdate(contract);
    }

    //删除
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return contractService.removeById(id);
    }

    //批量删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return contractService.removeBatchByIds(ids);
    }

    //查询所有数据
    @GetMapping
    public List<Contract> findAll() {
        return contractService.list();
    }

    //根据id查询
    @GetMapping("/{id}")
    public Contract findOne(@PathVariable Integer id) {
        return contractService.getById(id);
    }

    //分页查询
    @GetMapping("/page")
    public Page<Contract> findPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String landlordName,
                                   @RequestParam(defaultValue = "") String landlordCard,
                                   @RequestParam(defaultValue = "") String landlordPhone,
                                   @RequestParam(defaultValue = "") String renterName,
                                   @RequestParam(defaultValue = "") String renterCard,
                                   @RequestParam(defaultValue = "") String renterPhone,
                                   @RequestParam(defaultValue = "") String shopNum,
                                   @RequestParam(defaultValue = "") String address,
                                   @RequestParam(defaultValue = "") String purpose,
                                   @RequestParam(defaultValue = "") String price,
                                   @RequestParam(defaultValue = "") String leaseTerm,
                                   @RequestParam(defaultValue = "") String times,
                                   @RequestParam(defaultValue = "") String startTime,
                                   @RequestParam(defaultValue = "") String endTime,
                                   @RequestParam(defaultValue = "") String createTime
                                   ) {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(landlordName)) {
            queryWrapper.like("landlord_name", landlordName);
        }
        if (!"".equals(landlordCard)) {
            queryWrapper.like("landlord_card", landlordCard);
        }
        if (!"".equals(landlordPhone)) {
            queryWrapper.like("landlord_phone", landlordPhone);
        }
        if (!"".equals(renterName)) {
            queryWrapper.like("renter_name", renterName);
        }
        if (!"".equals(renterCard)) {
            queryWrapper.like("renter_card", renterCard);
        }
        if (!"".equals(renterPhone)) {
            queryWrapper.like("renter_phone", renterPhone);
        }
        if (!"".equals(shopNum)) {
            queryWrapper.like("shop_num", shopNum);
        }
        if (!"".equals(address)) {
            queryWrapper.like("address", address);
        }
        if (!"".equals(purpose)) {
            queryWrapper.like("purpose", purpose);
        }
        if (!"".equals(price)) {
            queryWrapper.like("price", price);
        }

        if (!"".equals(leaseTerm)) {
            queryWrapper.like("lease_term", leaseTerm);
        }
        if (!"".equals(times)) {
            queryWrapper.like("times", times);
        }
        if (!"".equals(startTime)) {
            queryWrapper.like("start_time", startTime);
        }
        if (!"".equals(endTime)) {
            queryWrapper.like("end_time", endTime);
        }
        if (!"".equals(createTime)) {
            queryWrapper.like("create_time", createTime);
        }
        return contractService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }


    @GetMapping("/download/{id}")
    public void downFilePDF(@PathVariable int id) throws IOException {
        Contract contract = this.findOne(id);
        String landlordName = contract.getLandlordName();
        String landlordCard = contract.getLandlordCard();
        String landlordPhone = contract.getLandlordPhone();
        String renterName = contract.getRenterName();
        String renterCard = contract.getRenterCard();
        String renterPhone = contract.getRenterPhone();
        String address = contract.getAddress();
        String purpose = contract.getPurpose();
        String price = contract.getPrice();
        String chinese = Main.integer2rmb(price);
        String leaseTerm = contract.getLeaseTerm();
        String times = contract.getTimes();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        LocalDate startTime1 =contract.getStartTime();
        LocalDate endTime1 = contract.getEndTime();
        String startTime = startTime1.format(df);
        String endTime = endTime1.format(df);
        PdfDocument pdfDocument = new PdfDocument(new PdfReader("C:\\Users\\S\\Desktop\\shop.pdf"), new PdfWriter("C:\\Users\\S\\Desktop\\合同\\"+landlordName+".pdf"));
        PdfAcroForm from = PdfAcroForm.getAcroForm(pdfDocument, false);
        PdfFont font = PdfFontFactory.createFont(FONT_PATH);
        DeviceRgb color = new DeviceRgb(0,0,0);
        Map<String,String> map =new HashMap<String,String>();
        map.put("landlordName",landlordName);
        map.put("landlordCard",landlordCard);
        map.put("landlordPhone",landlordPhone);
        map.put("renterName",renterName);
        map.put("renterCard",renterCard);
        map.put("renterPhone",renterPhone);
        map.put("address",address);
        map.put("purpose",purpose);
        map.put("price",price);
        map.put("chinese",chinese);
        map.put("leaseTerm",leaseTerm);
        map.put("times",times);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            from.getFormFields().get(entry.getKey()).setValue(entry.getValue()).setColor(color).setFont(font);
        }
        from.flattenFields();
        pdfDocument.close();
    }


}

